package com.lanxin.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Created by cyh on 2021/3/8.
 */
@Component
public class TokenFilter implements GlobalFilter,Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange serverWebExchange, GatewayFilterChain gatewayFilterChain) {
        String token = serverWebExchange.getRequest().getQueryParams().getFirst("token");

        if(token==null || "".equals(token)){
            serverWebExchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            System.out.println(token+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            return serverWebExchange.getResponse().setComplete();
        }
        return gatewayFilterChain.filter(serverWebExchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
